package com.elaticsearch.doc;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;

import java.io.IOException;

/**
 * @author:左泽林
 * @date:日期:2021-08-28-时间:19:54
 * @message:
 */
public class doc_query_height {
    /*public static void main(String[] args) throws IOException {
        //组合查询
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("172.20.17.43",9200,"http"))
        );

        try {
            //过滤字段
            SearchRequest search = new SearchRequest();
            //1.查询索引中全部的数据
            search.indices("user");

            SearchSourceBuilder builder = new SearchSourceBuilder();
            RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");

            rangeQuery.gte(30);
            rangeQuery.lt(50);

            builder.query(rangeQuery);

            search.source(builder);
            SearchResponse response = esClient.search(search, RequestOptions.DEFAULT);

            System.out.println("===============操作结果==============");
            System.out.println(response.getHits().getTotalHits());
            System.out.println(response.getTook());
            for (SearchHit hit : response.getHits()) {
                System.out.println(hit.getSourceAsString());
            }
            System.out.println("===============操作结果==============");
        }catch (Exception e){
            System.out.println(e);
        }finally {
            esClient.close();
        }




    }*/

    /*public static void main(String[] args) throws IOException {
        //组合查询
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("172.20.17.43",9200,"http"))
        );

        try {
            //过滤字段
            SearchRequest search = new SearchRequest();
            //1.查询索引中全部的数据
            search.indices("user");

            SearchSourceBuilder builder = new SearchSourceBuilder();
            //Fuzziness.ONE：差一个字符能匹配成功
            builder.query(QueryBuilders.fuzzyQuery("name", "wangwu").fuzziness(Fuzziness.ONE));

            search.source(builder);
            SearchResponse response = esClient.search(search, RequestOptions.DEFAULT);

            System.out.println("===============操作结果==============");
            System.out.println(response.getHits().getTotalHits());
            System.out.println(response.getTook());
            for (SearchHit hit : response.getHits()) {
                System.out.println(hit.getSourceAsString());
            }
            System.out.println("===============操作结果==============");
        }catch (Exception e){
            System.out.println(e);
        }finally {
            esClient.close();
        }




    }*/

    public static void main(String[] args) throws IOException {
        //高亮查询
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("172.20.17.43",9200,"http"))
        );

        try {
            //过滤字段
            SearchRequest search = new SearchRequest();
            //1.查询索引中全部的数据
            search.indices("user");

            SearchSourceBuilder builder = new SearchSourceBuilder();
            //Fuzziness.ONE：差一个字符能匹配成功
            TermsQueryBuilder termsQueryBuilder = QueryBuilders.termsQuery("name", "wangwu");

            HighlightBuilder highlightBuilder = new HighlightBuilder();
            highlightBuilder.preTags("<font color='red'>");
            highlightBuilder.postTags("</font>");
            highlightBuilder.field("name");

            builder.highlighter(highlightBuilder);
            builder.query(termsQueryBuilder);

            search.source(builder);
            SearchResponse response = esClient.search(search, RequestOptions.DEFAULT);

            System.out.println("===============操作结果==============");
            System.out.println(response.getHits().getTotalHits());
            System.out.println(response.getTook());
            for (SearchHit hit : response.getHits()) {
                System.out.println(hit.getSourceAsString());
            }
            System.out.println("===============操作结果==============");
        }catch (Exception e){
            System.out.println(e);
        }finally {
            esClient.close();
        }




    }
}
